package cc.minos.question.plugins
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	
	/**
	 * ...
	 * 画线
	 * groups: [[0,1,2,3],[4,5,6],[7,8]]
	 * answer: [[4],[5],[6],[6],[7],[7,8],[8]]
	 * @author Minos
	 */
	public class DrawLinePlugin extends ChoicePlugin
	{
		//API
		public static const VERSION : Number = 1.0;
		public static const API : Number = 2.0;
		
		//当前点击对象
		protected var currentChoice : MovieClip;
		//分组
		protected var groups : Array;
		//线条
		protected var lines : Shape;
		//储存画线
		protected var draweds : Array;
		//单线
		protected var isSingle : Boolean = true;
		
		/**
		 * 构造
		 */
		public function DrawLinePlugin()
		{
			this.propName = "DrawLinePlugin";
			//this.acronym = "line";
		}
		
		/**
		 * 初始
		 */
		override protected function init() : void
		{
			super.init();
			draweds = [];
			groups = getData( "groups" ) as Array;
			var s:String = getData("single")?String( getData("single") ):String( getData( "isSingle" ) )
			isSingle = s.toUpperCase() == "TRUE" ? true : false;
			lines = new Shape();
			skin.addChild( lines );
		}
		
		/**
		 * 更新状态
		 * @param	mc
		 */
		override protected function update( mc : MovieClip = null ) : void
		{
			super.update( mc );
			var clickChoice : MovieClip = mc;
			var index : int = getIndex( clickChoice.name );
			if ( !currentChoice )
			{
				currentChoice = clickChoice;
				choicedAry[ index ] = clickChoice;
				return;
			}
			
			var a : int = getGroup( currentChoice ) , b : int = getGroup( clickChoice );
			if ( Math.abs( a - b ) != 1 )
			{
				a = getIndex( currentChoice.name ) , b = getIndex( clickChoice.name );
				clearLineByIndex( b );
				if ( a != b )
					clearLineByIndex( a );
				currentChoice = null;
				return;
			}
			
			a = getIndex( currentChoice.name ) , b = getIndex( clickChoice.name );
			if ( !draweds[ Math.min( a , b ) ] )
				draweds[ Math.min( a , b ) ] = [];
			var _ary : Array = draweds[ Math.min( a , b ) ];
			if ( _ary.indexOf( Math.max( a , b ) ) == -1 )
			{
				if ( isSingle )
				{
					var groups : Array = groups[ getGroup( choices[ Math.min( a , b ) ] ) ];
					for ( var i : Object in groups )
					{
						var child : Array = draweds[ groups[ i ] ];
						if ( child && child[ 0 ] == Math.max( a , b ) )
							child.splice( 0 , 1 );
					}
					draweds[ Math.min( a , b ) ][ 0 ] = Math.max( a , b );
				}
				else
					_ary.push( Math.max( a , b ) );
			}
			updateLines();
			currentChoice = null;
		}
		
		/**
		 * 更新线条
		 */
		protected function updateLines() : void
		{
			var temp : Array = [];
			lines.graphics.clear();
			lines.graphics.lineStyle( 2 );
			//draw line
			for ( var i : Object in draweds )
			{
				if ( !draweds[ i ] )
					continue;
				for each ( var j : Object in draweds[ i ] )
				{
					lines.graphics.moveTo( choices[ i ].x , choices[ i ].y );
					lines.graphics.lineTo( choices[ j ].x , choices[ j ].y );
					//add to choiceed
					choicedAry[ i ] = choices[ i ] , choicedAry[ j ] = choices[ j ];
					temp[ i ] = i , temp[ j ] = j;
				}
			}
			//set style
			for ( i in choicedAry )
			{
				if ( temp[ i ] == i )
					choicedAry[ i ].gotoAndStop( 3 );
				else if ( choicedAry[ i ] )
				{
					choicedAry[ i ].gotoAndStop( 1 );
					choicedAry[ i ] = null;
				}
			}
		}
		
		/**
		 * 清除线条
		 * @param	index
		 */
		protected function clearLineByIndex( index : int , isUpdate : Boolean = true ) : void
		{
			var child : Array = draweds[ index ];
			if ( child != null && child.toString() != "" )
				draweds[ index ] = [];
			else
			{
				for ( var i : Object in draweds )
				{
					child = draweds[ i ];
					if ( child && child.indexOf( index ) != -1 )
						child.splice( child.indexOf( index ) , 1 );
				}
			}
			if ( isUpdate )
				updateLines();
		}
		
		/**
		 * 获取分组
		 * @param	mc
		 * @return
		 */
		protected function getGroup( mc : MovieClip = null ) : int
		{
			var index : int = getIndex( mc.name );
			var group : int = -1;
			for ( var i : int = 0 , len : int = groups.length ; i < len ; i++ )
			{
				if ( groups[ i ].indexOf( index ) != -1 )
				{
					group = i;
					break;
				}
			}
			return group;
		}
		
		/**
		 * 检查
		 * @return
		 */
		override public function check() : Boolean
		{
			for ( var i : Object in answer )
			{
				var d : Object = draweds[ i ];
				if ( d is Array )
					( d as Array ).sort();
				var a : Object = answer[ i ];
				if ( a is Array )
					( a as Array ).sort();
				
				if ( !d || d.toString() != a.toString() )
					return false;
			}
			return true;
		}
		
		/**
		 * 得分
		 */
		override public function get score() : Number
		{
			_score = 0;
			if ( checkAll )
				return check() ? tScore : 0;
			//
			for ( var i : int = 0 , len : int = answer.length ; i < len ; i++ )
			{
				if ( draweds[ i ] && answer[ i ].toString() == draweds[ i ].toString() )
					_score += tScore / len;
			}
			//
			return _score;
		}
		
		/**
		 * 重置
		 */
		override public function reset() : void
		{
			super.reset();
			draweds = [];
			choicedAry = [];
			lines.graphics.clear();
		}
	
	}

}